﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using Aga.ProofOfConcepts.Helpers;

namespace Aga.ProofOfConcepts
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class DatabaseSchemaPoc
    {
        public DatabaseSchemaPoc()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private static TableSchema CreateSampleSchema()
        {
            // Create the field definitions
            TableField id = new TableField { Name = "Id", Type = SqlDbType.Int, Identity = true, Null = false };
            TableField name = new TableField { Name = "Name", Type = SqlDbType.NVarChar, Length = 50, Null = false };
            TableField value = new TableField { Name = "Value", Type = SqlDbType.Decimal, Precision = 10, Scale = 2, DefaultValue = "2.0", Null = true };

            // Create the table schema
            TableSchema table = new TableSchema { Name = "TestTable" };

            // Adds the fields to the table
            table.Key.Add(id);
            table.Fields.AddRange(new[] { id, name, value });
            return table;
        }

        [TestMethod]
        public void DefineDatabaseSchema()
        {
            TableSchema table = CreateSampleSchema();

            XmlHelper helper = new XmlHelper();

            string serializedSchema = helper.SerializeObject<TableSchema>(table);

            TableSchema deserializedSchema = helper.DeserializeObject<TableSchema>(serializedSchema);

            string serializedSchema2 = helper.SerializeObject<TableSchema>(deserializedSchema);

            Assert.AreEqual(serializedSchema, serializedSchema2);
        }

        [TestMethod]
        public void CreateTableFromSchema()
        {
            TableSchema table = CreateSampleSchema();

            DBManager dbManager = new DBManager();

            dbManager.ConnectionString = "Data Source=(local);Initial Catalog=Pruebas;Integrated Security=True";

            if (!dbManager.TableExists(table.Name))
            {
                dbManager.CreateTable(table);
            }

            Assert.IsTrue(dbManager.TableExists(table.Name));

            if (dbManager.TableExists(table.Name))
            {
                dbManager.DropTable(table);
            }

            Assert.IsFalse(dbManager.TableExists(table.Name));

        }
    }

}